#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include <fcntl.h>

int main()
{
    char command[1024];
    char* output_file = NULL;
    
    if(fgets(command, sizeof(command), stdin) == NULL)
    {
        perror("fgets failed");
        return 1;
    }

    if(command[strlen(command) - 1] == '\n')
    {
        command[strlen(command) - 1] = '\0';
    }

    char* output_redirect = strchr(command, '>');

    if(output_redirect)
    {
        *output_redirect = '\0';
        output_redirect++;
        while(*output_redirect == ' ') output_redirect++;
        output_file = output_redirect;
    }

    if(output_file)
    {
        int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
        if (fd < 0) {
            perror("open failed");
            return 1;
        }
        dup2(fd, STDOUT_FILENO);
        close(fd);
    }

    printf("%s\n", command);
    return 0;
}
